﻿import { Uploader } from './Uploder.js';

const uploader = new Uploader({
    uploadUrl: '/file/DoUpload',
    progressElement: '#progressStrip',
    sliderElement: '#ID-slider-demo',
    shardSize: 28 * 1024 * 1024 // 分片大小，限制大小不能超过29M

});
uploader.doneFunc = function (d) {
    //上传成功后回调函数
    layui.layer.msg("上传成功,三秒后刷新")
    $("#progressStrip").html("完成");
    var searchParams = new URLSearchParams(window.location.search);
    var ParamValue = searchParams.get('dir');
    setTimeout(function () {
        window.parent.GetFileList(ParamValue);
    }, 500);

    console.log("上传成功后回调函数:" + d);
    $("#btnDoUpload").removeAttr("disabled");
}
$("#btnDoUpload").click(async function () {
    const fileInfo = $("#file")[0].files[0];
    if (!fileInfo) {
        layui.layer.msg("请选择文件！");
        return;
    }
    $("#btnDoUpload").attr("disabled", "disabled");

    try {
        await uploader.upload([fileInfo]);
    } catch (error) {
        console.error('Upload error:', error);
        $("#btnDoUpload").removeAttr("disabled");
    }
});

//文件选择
$("#file").on("change", function () {
    let fileInfo = $("#file")[0].files[0];
    let kb = parseFloat(fileInfo.size / 1024).toFixed(2);
    console.log("文件大小kb=" + kb);

    let sizeInfo = "";
    if (kb < 1024) {
        sizeInfo = kb + " KB";
    }
    else {
        let mb = parseFloat(kb / 1024).toFixed(2);

        if (mb < 1024) {
            sizeInfo = mb + " MB";
        }
        else {
            let gb = parseFloat(mb / 1024).toFixed(2);
            sizeInfo = gb + " GB";
        }
    }
    $("#fileInfo").html(fileInfo.name + "<br/>文件大小：" + sizeInfo);
});
